#include<bits/stdc++.h>
using namespace std;
int t;
int n;
int a[200002];
bool b[200002];
int ans;
int dp[200002];
int main(){
    freopen("color.in","r",stdin);
    freopen("color.out","w",stdout);
    scanf("%d",&t);
    while(t--){
        memset(dp,0,sizeof(dp));
        memset(b,0,sizeof(b));
        scanf("%d",&n);
        for(int i=1; i<=n; i++){
            scanf("%d",&a[i]);
        }
        for(int i=n; i>1; i--){
            for(int j=n-1; j>=1; j--){
                if(a[j]==a[i]){
                    dp[j]=max(dp[j+1]+a[i],dp[j]);
                }
            }
        }
        printf("%d",dp[1]);
    }
    return 0;
}
